$(function() {
    $('form').parsley();
    dynamicBinding();
    getAllForeignVesselNumber();
});
//遍历所有foreignVesselNumber输出到表格
function getAllForeignVesselNumber() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {title: "序号", data: null,"render": function (data,type,row,meta) {
                return data=Number(meta.row)+Number(1);
            }},
        <!--String helicopterName, BigDecimal dailyFee, BigDecimal fuelPrice, String rescueArea, Date rescueBeginTime, Date rescueEndTime,BigDecimal rescueFuel,Long rescueNum-->
        {title: "船舶名称", data: "shipName"},
        {title: "日费", data: "dailyFee"},
        {title: "燃油价格", data: "fuelPrice"},
        {title: "搜救海区", data: "rescueArea"},
        {title: "搜救开始时间", data: "rescueBeginTime"},
        {title: "搜救结束时间", data: "rescueEndTime"},
        {title: "搜救油耗", data: "rescueFuel"},
        {title: "搜救人数", data: "rescueNum"},
        {title: "操作", data: "id","orderable": false, // 禁用排序
            "defaultContent": "","render": function (data,type,row,meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateForeignVesselNumberModal'>更新</button>";
                }
                return data = deleteStr+updateStr;}}
    ];
    let url ='foreignVesselNumber/getAllForeignVesselNumber';
    dataTableInit(url,columns);
}


//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind('/foreignVesselNumber/deleteForeignVesselNumber');
    //为所有的update按钮绑定update事件
    $("#datatable").on('click', ".data-update", function () {
        let data_id = $(this).attr('data-id');
        //获取此行数据
        let row = $("#datatable").DataTable().row($(this).parents('tr')).data();
        //遍历更新表单的所有拥有name的输入框，将行数据填充到这些输入框中，file除外
        $('#toUpdateForeignVesselNumber'+" [name]").each(function () {
            if($(this).attr('type')!=="file"){
                let name =  $(this).attr("name");
                $(this).val(row[name]);
            }
        });
        $('#toUpdateForeignVesselNumber').parsley().on('form:validate', function () {
        }).on('form:submit', function () {
            let id = data_id;
            let data = new FormData($('#toUpdateForeignVesselNumber')[0]);
            data.set('rescueBeginTime',Date($(" #updateRescueBeginTime ").val()));
            data.set('rescueEndTime',Date($(" #updateRescueEndTime ").val()));
            data.set("id", id);
            updateAjax(data, '/foreignVesselNumber/updateForeignVesselNumber', false, false, "#updateForeignVesselNumberModal");
            return false;
        });
    });
    //为所有的add按钮绑定add事件
    $('#toAddForeignVesselNumber').parsley().on('form:validate', function () {
    }).on('form:submit', function () {
        let data = new FormData($('#toAddForeignVesselNumber')[0]);
        data.set('rescueBeginTime',Date($(" #addRescueBeginTime ").val()));
        data.set('rescueEndTime',Date($(" #addRescueEndTime ").val()));
        addAjax(data, '/foreignVesselNumber/addForeignVesselNumber', false, false, "#addForeignVesselNumberModal");
        return false;
    });
    //为add模态框绑定消失事件-清空模态框中内容
    $('#addForeignVesselNumberModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddForeignVesselNumber").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateForeignVesselNumberModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdateForeignVesselNumber").reset();
    });


   /* //为所有的delete按钮绑定delete事件
    deleteBind("#foreignVesselNumber-table-body",".data-delete","data-id",'/foreignVesselNumber/deleteForeignVesselNumber');
    //为所有的update按钮绑定update事件
    <!--String helicopterName, BigDecimal dailyFee, BigDecimal fuelPrice, String rescueArea, Date rescueBeginTime, Date rescueEndTime,BigDecimal rescueFuel,Long rescueNum-->
    $("#foreignVesselNumber-table-body").on('click', ".data-update", function(){
        let _id=$(this).attr('data-id');
        $("#toUpdateForeignVesselNumber").ajaxForm(function () {
            let id = _id;
            let shipName = $(" #updateShiprName ").val();
            let dailyFee = new Number($(" #updateDailyFee ").val());
            let fuelPrice = new Number($(" #updateFuelPrice ").val());
            let rescueArea = $(" #updateRescueArea ").val();
            let rescueBeginTime = new Date($(" #updateRescueBeginTime ").val());
            let rescueEndTime = new Date($(" #updateRescueEndTime ").val());
            let rescueFuel = new Number($(" #updateRescueFuel ").val());
            let rescueNum = new Number($(" #updateRescueNum ").val());
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/foreignVesselNumber/updateForeignVesselNumber',
                data: { id: id,shipName:shipName,dailyFee:dailyFee, fuelPrice:fuelPrice, rescueArea:rescueArea, rescueBeginTime:rescueBeginTime, rescueEndTime:rescueEndTime,
                    rescueFuel:rescueFuel,rescueNum:rescueNum},
                success: function (result) {
                    if (result.code==200){
                        window.location.reload();
                    }else {
                        console.log(result.msg);
                    }
                },
                error: function (result) {
                    return result;
                }
            })
        });
    });
    //为add按钮绑定add事件
    $("#toAddForeignVesselNumber").on('click', function(){
        $("#toAddForeignVesselNumber").ajaxForm(function () {
            let shipName = $(" #addShipName ").val();
            let dailyFee = new Number($(" #addDailyFee ").val());
            let fuelPrice = new Number($(" #addFuelPrice ").val());
            let rescueArea = $(" #addRescueArea ").val();
            let rescueBeginTime = new Date($(" #addRescueBeginTime ").val());
            let rescueEndTime = new Date($(" #addRescueEndTime ").val());
            let rescueFuel = new Number($(" #addRescueFuel ").val());
            let rescueNum = new Number($(" #addRescueNum ").val());
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/foreignVesselNumber/addForeignVesselNumber',
                data: { shipName:shipName,dailyFee:dailyFee, fuelPrice:fuelPrice, rescueArea:rescueArea, rescueBeginTime:rescueBeginTime, rescueEndTime:rescueEndTime,
                    rescueFuel:rescueFuel,rescueNum:rescueNum},
                success: function (result) {
                    if(result.code==200){
                        window.location.reload();
                    }else{
                        console.log(result.msg);
                    }
                },
                error: function (result) {
                    return result;
                }
            })
        });
    });*/
}
